﻿using System.Runtime.InteropServices;

namespace WorkTool.Helpers
{
    public static class EscapeListener
    {
        [DllImport("user32.dll")]
        private static extern short GetAsyncKeyState(int vKey);

        public static bool _isRunning = true;

        public static void CheckForEscapeKey()
        {
            while (_isRunning)
            {
                if (IsKeyPressed(ConsoleKey.Escape))
                {
                    Console.WriteLine("\n检测到ESC键，程序退出...");
                    // _isRunning = false;
                    // Environment.Exit(0);
                }
                Thread.Sleep(100);
            }
        }

        public static bool IsKeyPressed(ConsoleKey key)
        {
            return (GetAsyncKeyState((int)key) & 0x8000) != 0;
        }
    }
}